﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Resources;
using SAPbobsCOM;
using BoAddonBiz_FreightForwarder.BusinessBase;

namespace BoAddonBiz_FreightForwarder
{
    [ControlB1FormAttribute(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_ConditionTemp)]
    class B1Form_ConditionTemplete : SAPBoAddon.B1Form
    {
        public B1Form_ConditionTemplete()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_ConditionTemplete_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_ConditionTemplete_frm_ChooseFormListEvent);
            this.frm_MenuEvent += new SAPBoAddon.IB1FormEvents.B1MenuEventEventHandler(B1Form_ConditionTemplete_frm_MenuEvent);
        }
        #region 事件
        void B1Form_ConditionTemplete_frm_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
        }
        void B1Form_ConditionTemplete_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction)
            { }
            else
            {
                switch (pVal.ChooseFromListUID)
                {
                    case "CFL_0":
                        SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFCT");
                        dbds.SetValue("U_FeeCode", dbds.Offset, pVal.SelectedObjects.GetValue("ItemCode", 0).ToString());
                        dbds.SetValue("U_FeeName", dbds.Offset, pVal.SelectedObjects.GetValue("ItemName", 0).ToString());
                        break;
                }
            }
        }
        void B1Form_ConditionTemplete_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
            }
            else
            {
                if (pVal.ItemUID == "mtx_0" && pVal.ColUID == "col_17" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                {
                    string DocDate = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFCT").GetValue("U_DocDate", 0);
                    Utils.SetRateByCurrency(this.CurrentForm, SBOCompany, "mtx_0", "col_17", "col_18", pVal.Row, DocDate);
                }
                if (pVal.ItemUID == "mtx_0" && pVal.ColUID == "col_1" && pVal.EventType == BoEventTypes.et_VALIDATE)
                {
                    SAPBoAddon.B1Assistant.Matrix.AutoAddNewLine(this.CurrentForm, "mtx_0", "col_f", "@AVA_FF_FCT1", -1, "Lineid", "Lineid", true, "col_1");
                }
            }
        }
        #endregion
        #region 方法
        /// <summary>
        /// 验证当前费用条件对应的费用编码不能已经存在，否则费用方案中不知道选中哪项作为加载项
        /// </summary>
        /// <returns></returns>
        /// <author>Webb.Wen</author>
        /// <create>2011-09-26 10:14:11</create>
        private bool CheckItemCode()
        {
            string ItemCode = this.CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFCT").GetValue("U_FeeCode", 0).Trim();
            if (string.IsNullOrEmpty(ItemCode))
            {
                SBOApp.MessageBox("请选择费用项");
                return false;
            }
            Recordset rs = this.SBOCompany.GetBusinessObject(BoObjectTypes.BoRecordset);
            string select = String.Format("select COUNT(*) as C from [@AVA_FF_OFCT] where U_FeeCode= '{0}'", ItemCode);
            rs.DoQuery(select);
            if (rs.Fields.Item("C").Value.ToString().Trim() == "0")
            {
                return true;
            }
            SBOApp.MessageBox("此费用项已在系统中创建了条件模板，请使用查找进行查看。");
            return false;
        }
        #endregion
        #region 重载
        protected override bool BeforeSaveCheckFormData()
        {
            return CheckItemCode();
        }
        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {
            switch (frmEditMode)
            {
                case SAPBoAddon.frmItemEditMode.fe_AddMode:
                    SAPbouiCOM.DBDataSource dbds = CurrentForm.DataSources.DBDataSources.Item("@AVA_FF_OFCT");
                    dbds.SetValue("U_DocDate", dbds.Offset, DateTime.Now.ToString("yyyyMMdd"));
                    this.Matrix_AutoAddLine();
                    break;
            }
        }
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_ConditionTemp;
            FormSetting.FormFileName = B1FormTypes.ado_AVA_FF_ConditionTemp.ToString() + "Form.srf";

            FormSetting.AutoGetObjectNextNumber = true;

            FormSetting.MatrixSetting.MtxUID = "mtx_0";
            FormSetting.MatrixSetting.AutoAddNewLine = true;
            FormSetting.MatrixSetting.NotAllowNoLineSave = false;
            FormSetting.MatrixSetting.AutoLineKeyCol = "col_1";
            FormSetting.MatrixSetting.BeforeSaveFixDbDataSourceData = false;
            FormSetting.MatrixSetting.BeforeSaveDeleteNoValueLine = true;
            FormSetting.MatrixSetting.TableKeyField = "LineId";
            FormSetting.MatrixSetting.TableVisOrder = "VisOrder";
            FormSetting.MatrixSetting.PrimaryLineCol = "col_f";
            FormSetting.MatrixSetting.TableName = "@AVA_FF_FCT1";
            FormSetting.MatrixSetting.Add();
        }
        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialMatrixComboBox(this.SBOCompany, this.CurrentForm.Items.Item("mtx_0").Specific, "col_17", "DocCurrCod", "CurrName", "OCRN");//订单金额币种
        }
        #endregion
    }
}
